package extend_widget

import (
	"clientDemo/ui/fyne_ui/common"
	"clientDemo/ui/fyne_ui/widget"
	"clientDemo/ui/fyne_ui/widget/interface"
	"fyne.io/fyne/v2"
	widget2 "fyne.io/fyne/v2/widget"
)

var _ widgetcommon.IWidgetItem = (*Button)(nil)

func init() {
	//widget.RegWidget("Button", NewButton)
	//
	widget.RegWidget("ButtonEmpty1", "按钮", NewButton)
}

type Button struct {
	*WidgetContainer
	Button *widget2.Button
	*Action
	*ButtonEvent
	rootNode widgetcommon.IWidgetItem //父节点
}

func NewButton(name string, skinInfo widgetcommon.IWidgetSkin, rootNode widgetcommon.IWidgetItem) widgetcommon.IWidgetItem {
	if skinInfo == nil {
		return nil
	}
	o := widget2.NewButton(name, func() {
	})
	_button := &Button{
		Button:      o,
		Action:      &Action{},
		ButtonEvent: &ButtonEvent{},
		WidgetContainer: &WidgetContainer{
			Container: &Container{
				name: name,
			},
		},
		rootNode: rootNode,
	}
	_button.WidgetContainer.ac = _button.Action
	_button.IUserInfo = common.NewWidgetUserInfo(name, skinInfo.GetSkinName(), _button)
	_button.WidgetContainer.SetObjects([]fyne.CanvasObject{_button.Button})
	_button.SetBase(_button, rootNode)
	_button.ExtendBaseWidget(_button)
	_button.Button.Text = name
	_button.OnTapped = buttonCallBack(name, _button)
	return _button
}

func buttonCallBack(name string, item widgetcommon.IWidgetItem) func() {
	return func() {
		cb := widget.GetButtonCallBack(name)
		cb(item, item.GetUserInfo())
	}
}
func (i *Button) Tapped(_ *fyne.PointEvent) {
	if onTapped := i.OnTapped; onTapped != nil {
		onTapped()
	}
}
